﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Diagnostics;
using FotoAnotSharp.Anotator;

namespace FotoAnotSharp
{
    /// <summary>
    /// This class load and save only polygons with anotations
    /// </summary>
    public class GateAnotatedFile
    {
        public List<GateAnotatedFileItem> Objects { get; set; }

        public AnotFile Anotation { get; set; }

        public static void Save(GateAnotatedFile file, string fileName)
        {
            XmlSerializer ser = new XmlSerializer(typeof(GateAnotatedFile));

            try
            {
                using (FileStream str = new FileStream(fileName, FileMode.Create))
                {
                    ser.Serialize(str, file);
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Soubor se nepovedlo uložit.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Debug.WriteLine(er.Message + er.StackTrace);
            }
        }

        public static GateAnotatedFile Load(string fileName)
        {
            XmlSerializer ser = new XmlSerializer(typeof(GateAnotatedFile));
            try
            {
                using (FileStream str = new FileStream(fileName, FileMode.Open))
                {
                    GateAnotatedFile ret = (GateAnotatedFile)ser.Deserialize(str);

                    return ret;
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Soubor se nepovedlo otevřít.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Debug.WriteLine(er.Message + er.StackTrace);
                return null;
            }
        }
    }


    public class GateAnotatedFileItem
    {
        public int AnotId { get; set; }
        public string GateItem { get; set; }
        public string Name { get; set; }

        public List<Label> Labels { get; set; }

        public class Label
        {
            [XmlIgnore]
            public System.Globalization.CultureInfo Info { get; set; }

            public string CultureCode { get { return Info.TwoLetterISOLanguageName; } 
                set {
                    try
                    {
                        Info = new System.Globalization.CultureInfo(value);
                    }
                    catch (Exception er)
                    {
                        Trace.TraceError(er.Message + er.StackTrace);
                    }
                }
            }
            [XmlIgnore]
            public string LabelText { get; set; }

            private static readonly XmlDocument _xmlDoc = new XmlDocument();
            public XmlCDataSection LabelData
            {
                get { return _xmlDoc.CreateCDataSection(LabelText); }
                set { LabelText = value.Data.Replace("\n", "\r\n").Replace("\r\r", "\r"); }
            }
        }
    }
}
